package com.lumacong.websocket.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lumacong.websocket.service.DsPrizeUserService;
import com.lumacong.websocket.util.CaiJian;
import com.lumacong.websocket.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.net.URISyntaxException;

@Controller
@RequestMapping("/douScreenCapture")
public class DouScreenCaptureController {
    @Autowired
    private DsPrizeUserService dsPrizeUserService;

    @RequestMapping("/index")
    @ResponseBody
    public Object index() {
        String path = "D:\\root_server\\tomcat7042\\webapps\\";
        String data = null;
        boolean result = false;
                CaiJian cj = new CaiJian();
        String imgName = cj.screenShots(path);
        if (imgName != null) {
            path += imgName;
            try {
                data = Check.checkFile(path);
                System.out.println("~~~~~~~~~" + data);
                JSONObject jsonObject = JSON.parseObject(data);
                String logId = jsonObject.getString("log_id");
                String wordsResultNum = jsonObject.getString("words_result_num");
                JSONArray wordsResult = jsonObject.getJSONArray("words_result");
                String imgUrl = imgName;
                String wordsResultStr = wordsResult.toJSONString();
                result = dsPrizeUserService.insertByStatement(logId, wordsResultNum, imgUrl, wordsResultStr);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
